﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Benefit.Models.View;

namespace Benefit.Service.View
{
    /// <summary>
    /// 交易员持仓时间和收益率关系图
    /// </summary>
    public class ServiceOperatorYieldInHoldTime : Benefit.Interface.View.IOperatorYieldInHoldTime
    {
        Benefit.DB.DBManager db = null;
        public ServiceOperatorYieldInHoldTime(Benefit.DB.DBManager db)
        {
            this.db = db;
        }






        public List<OperatorYieldInHoldTime> GetOperatorYieldInHoldTime(int tradeHistoryId, int operatorId)
        {
            Interface.Sys.IDayOperatorAccount idoa = new Sys.ServiceDayOperatorAccount(db);
            Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
            Interface.Day.IYieldInHoldTime iyiht = new Day.ServiceYieldInHoldTime(db);
            List<OperatorYieldInHoldTime> times = new List<OperatorYieldInHoldTime>();
            List<Models.Sys.Account> accounts = idoa.GetDayOperatorAccounts(ith.GetDateTimeFromTradeHistoryId(tradeHistoryId), operatorId);
            foreach (Models.Sys.Account account in accounts)
            {
                List<Models.Day.YieldInHoldTime> lists = iyiht.GetYieldInHoldTime(tradeHistoryId, account.Id);
                foreach (Models.Day.YieldInHoldTime list in lists)
                {
                    var query = times.Where(a => a.HoldTime == list.HoldTime);
                    if (query.Count() > 0)
                    {
                        var obj = query.First();
                        obj.Yield += list.Yield;
                    }
                    else
                    {
                        OperatorYieldInHoldTime time = new OperatorYieldInHoldTime();
                        time.HoldTime = list.HoldTime;
                        time.OperatorId = operatorId;
                        time.Yield = list.Yield;
                        times.Add(time);
                    }
                }
            }
            times.Sort((x, y) => x.HoldTime.CompareTo(y.HoldTime));
            return times;
        }
    }
}
